//----------------------------------------------------------------------------------------
// @Project Includes
#include "includes.h"

//----------------------------------------------------------------------------------------
// @Local Macro Define in the module

//----------------------------------------------------------------------------------------
// @Local Type defines in the module

//----------------------------------------------------------------------------------------
// @Global Variables define in the module

//----------------------------------------------------------------------------------------
// @Prototypes of functions in the module
static void drv_gpio_exitInit(GPIO_Module* GPIOx, uint16_t pin,uint8_t portSource, uint8_t pinSource,uint32_t extiLine,IRQn_Type nvicIrqChannel,FunctionalState Cmd);

//----------------------------------------------------------------------------------------
/**
 * @brief  External lines 3 interrupt.
 * @param  none
 * @return none
 * @note   none
 */ 
void EXTI3_IRQHandler(void)
{
    if (RESET != EXTI_GetITStatus(CHG_INPUT_EXTI_LINE))    
    {		
				g_stcVar.enWakeupSource = WAKEUP_CHARGE;			
        EXTI_ClrITPendBit(CHG_INPUT_EXTI_LINE);   
    }		
}

/**
 * @brief  External lines 4 interrupt.
 * @param  none
 * @return none
 * @note   none
 */ 
void EXTI4_IRQHandler(void)
{
    if (RESET != EXTI_GetITStatus(LOAD_INPUT_EXTI_LINE))    
    {	
				g_stcVar.enWakeupSource = WAKEUP_LOAD;			
        EXTI_ClrITPendBit(LOAD_INPUT_EXTI_LINE);   
    }				
}

/**
 * @brief  External lines 9_5 interrupt.
 * @param  none
 * @return none
 * @note   none
 */ 
void EXTI9_5_IRQHandler(void)
{
    if (RESET != EXTI_GetITStatus(CRG_INPUT_EXTI_LINE))    
    {	
				g_stcVar.enWakeupSource = WAKEUP_SW;			
        EXTI_ClrITPendBit(CRG_INPUT_EXTI_LINE);   
    }		
    if (RESET != EXTI_GetITStatus(SW_INPUT_EXTI_LINE))    
    {	
				g_stcVar.enWakeupSource = WAKEUP_SW;			
        EXTI_ClrITPendBit(SW_INPUT_EXTI_LINE);   
    }			

    if (RESET != EXTI_GetITStatus(DI_INPUT_EXTI_LINE))    
    {
				g_stcVar.enWakeupSource = WAKEUP_SW;			
        EXTI_ClrITPendBit(DI_INPUT_EXTI_LINE);   
    }	
		
		
}

/**
 * @brief  External lines 15_10 interrupt.
 * @param  none
 * @return none
 * @note   none
 */ 
void EXTI15_10_IRQHandler(void)
{
    if (RESET != EXTI_GetITStatus(COM_INPUT_EXTI_LINE))    
    {	
				g_stcVar.enWakeupSource = WAKEUP_COMMUN;		
        EXTI_ClrITPendBit(COM_INPUT_EXTI_LINE);   
    }		
}

/**
 * @brief  driver gpio exti initialization
 * @param  GPIOx :GPIO Module
 *         pin: the pin nume
 *         portSource: port source
 *         pinSource: pin source
 *         extiLine: exti line
 *         nvicIrqChannel: nvic irq channel
 * @return none
 * @note   none
 */
static void drv_gpio_exitInit(GPIO_Module* GPIOx, uint16_t pin,uint8_t portSource, uint8_t pinSource,uint32_t extiLine,IRQn_Type nvicIrqChannel,FunctionalState Cmd)
{
    GPIO_InitType GPIO_InitStructure;
    EXTI_InitType EXTI_InitStructure;
    NVIC_InitType NVIC_InitStructure;

    /* Check the parameters */
    assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

    /* Enable the GPIO Clock */
    if (GPIOx == GPIOA)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO, ENABLE);
    }
    else if (GPIOx == GPIOB)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);
    }
    else if (GPIOx == GPIOC)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC | RCC_APB2_PERIPH_AFIO, ENABLE);
    }
    else
    {
        if (GPIOx == GPIOD)
        {
            RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOD | RCC_APB2_PERIPH_AFIO, ENABLE);
        }
				else
				{
						return ;
				}
    }

    /*Configure the GPIO pin as input floating*/
    if (pin <= GPIO_PIN_ALL)
    {
        GPIO_InitStruct(&GPIO_InitStructure);
        GPIO_InitStructure.Pin         = pin;
			  GPIO_InitStructure.GPIO_Mode   = GPIO_Mode_Input;
			  if (pinSource == CHG_INPUT_PIN_SOURCE)
				{
					GPIO_InitStructure.GPIO_Pull   = GPIO_Pull_Down;
				}
				else
				{
					GPIO_InitStructure.GPIO_Pull   = GPIO_Pull_Up;
				}
        GPIO_InitPeripheral(GPIOx, &GPIO_InitStructure);
    }

    /*Configure key EXTI Line to key input Pin*/
    GPIO_ConfigEXTILine(portSource, pinSource);

    /*Configure key EXTI line*/
    EXTI_InitStructure.EXTI_Line    = extiLine;
    EXTI_InitStructure.EXTI_Mode    = EXTI_Mode_Interrupt;
		if (pinSource == CHG_INPUT_PIN_SOURCE)
		{
				EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; // EXTI_Trigger_Rising;
		}
		else
		{
				EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; // EXTI_Trigger_Rising;
		}
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_InitPeripheral(&EXTI_InitStructure);
		
	  /*Set key input interrupt priority*/
	  NVIC_InitStructure.NVIC_IRQChannel                   = nvicIrqChannel;
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
		NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 6;
	  NVIC_InitStructure.NVIC_IRQChannelCmd                = Cmd;
	  NVIC_Init(&NVIC_InitStructure);

}

/**
 * @brief  driver gpio initialization
 * @param  GPIOx :GPIO Module
 *         pin: the pin nume
  *        gpio_mode: the gpio mode
 * @return none
 * @note   none
 */
void drv_gpio_outputInit (GPIO_Module* GPIOx, uint16_t pin,GPIO_ModeType gpio_mode)
{
    GPIO_InitType GPIO_InitStructure;

    /* Check the parameters */
    assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

    /* Enable the GPIO Clock */
    if (GPIOx == GPIOA)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
    }
    else if (GPIOx == GPIOB)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
    }
    else if (GPIOx == GPIOC)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
    }
    else
    {
        if (GPIOx == GPIOD)
        {
            RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOD, ENABLE);
        }
				else
				{
						return ;
				}
				
    }

    /* Configure the GPIO pin */
    if (pin <= GPIO_PIN_ALL)
    {
        GPIO_InitStruct(&GPIO_InitStructure);
        GPIO_InitStructure.Pin = pin;
        GPIO_InitStructure.GPIO_Mode = gpio_mode;
        GPIO_InitPeripheral(GPIOx, &GPIO_InitStructure);
    }	
}

/**
 * @brief  driver gpio sleep
 * @param  none
 * @return none
 * @note   none
 */
void drv_gpio_sleep(void)
{
		LED_RESET();
//		COMPOWER_RESET();	
		PCHG_RESET();
		BUZ_RESET();
		VBAT_RESET();
	
		drv_gpio_exitInit(CHG_INPUT_PORT, CHG_INPUT_PIN,CHG_INPUT_PORT_SOURCE, CHG_INPUT_PIN_SOURCE ,CHG_INPUT_EXTI_LINE,CHG_INPUT_IRQN,ENABLE);
		drv_gpio_exitInit(LOAD_INPUT_PORT, LOAD_INPUT_PIN,LOAD_INPUT_PORT_SOURCE, LOAD_INPUT_PIN_SOURCE ,LOAD_INPUT_EXTI_LINE,LOAD_INPUT_IRQN,ENABLE);
    drv_gpio_exitInit(COM_INPUT_PORT, COM_INPUT_PIN,COM_INPUT_PORT_SOURCE, COM_INPUT_PIN_SOURCE ,COM_INPUT_EXTI_LINE,COM_INPUT_IRQN,ENABLE);		

    drv_gpio_exitInit(SW_INPUT_PORT, SW_INPUT_PIN,SW_INPUT_PORT_SOURCE, SW_INPUT_PIN_SOURCE ,SW_INPUT_EXTI_LINE,SW_INPUT_IRQN,ENABLE);		
		drv_gpio_exitInit(CRG_INPUT_PORT, CRG_INPUT_PIN,CRG_INPUT_PORT_SOURCE, CRG_INPUT_PIN_SOURCE ,CRG_INPUT_EXTI_LINE,CRG_INPUT_IRQN,ENABLE);			
    drv_gpio_exitInit(DI_INPUT_PORT, DI_INPUT_PIN,DI_INPUT_PORT_SOURCE, DI_INPUT_PIN_SOURCE ,DI_INPUT_EXTI_LINE,DI_INPUT_IRQN,ENABLE);	
}

/**
 * @brief  driver gpio wakeup
 * @param  none
 * @return none
 * @note   none
 */
void drv_gpio_wakeup(void)
{
	  drv_gpio_init();
}

/**
 * @brief  driver gpio initialization
 * @param  none
 * @return none
 * @note   none
 */
void drv_gpio_init(void)
{
	  drv_gpio_outputInit (GPIO_PORT_LED, GPIO_PIN_LED,GPIO_Mode_Out_PP);
		drv_gpio_outputInit (GPIO_PORT_COMPOWER, GPIO_PIN_COMPOWER,GPIO_Mode_Out_PP);	
	  drv_gpio_outputInit (GPIO_PORT_PCHG, GPIO_PIN_PCHG,GPIO_Mode_Out_PP);
	  drv_gpio_outputInit (GPIO_PORT_BUZ, GPIO_PIN_BUZ,GPIO_Mode_Out_PP);
	  drv_gpio_outputInit (GPIO_PORT_VBAT, GPIO_PIN_VBAT,GPIO_Mode_Out_PP);		
	
		LED_RESET();
		COMPOWER_SET();	
		PCHG_RESET();
		BUZ_RESET();
		VBAT_SET();
	
		drv_gpio_exitInit(CHG_INPUT_PORT, CHG_INPUT_PIN,CHG_INPUT_PORT_SOURCE, CHG_INPUT_PIN_SOURCE ,CHG_INPUT_EXTI_LINE,CHG_INPUT_IRQN,DISABLE);
		drv_gpio_exitInit(LOAD_INPUT_PORT, LOAD_INPUT_PIN,LOAD_INPUT_PORT_SOURCE, LOAD_INPUT_PIN_SOURCE ,LOAD_INPUT_EXTI_LINE,LOAD_INPUT_IRQN,DISABLE);
    drv_gpio_exitInit(COM_INPUT_PORT, COM_INPUT_PIN,COM_INPUT_PORT_SOURCE, COM_INPUT_PIN_SOURCE ,COM_INPUT_EXTI_LINE,COM_INPUT_IRQN,DISABLE);		

    drv_gpio_exitInit(SW_INPUT_PORT, SW_INPUT_PIN,SW_INPUT_PORT_SOURCE, SW_INPUT_PIN_SOURCE ,SW_INPUT_EXTI_LINE,SW_INPUT_IRQN,DISABLE);		
		drv_gpio_exitInit(CRG_INPUT_PORT, CRG_INPUT_PIN,CRG_INPUT_PORT_SOURCE, CRG_INPUT_PIN_SOURCE ,CRG_INPUT_EXTI_LINE,CRG_INPUT_IRQN,DISABLE);			
    drv_gpio_exitInit(DI_INPUT_PORT, DI_INPUT_PIN,DI_INPUT_PORT_SOURCE, DI_INPUT_PIN_SOURCE ,DI_INPUT_EXTI_LINE,DI_INPUT_IRQN,DISABLE);		
}

// =======================================================================================
// End of file.
// =======================================================================================
